package com.cloudsigns.factorymanager2.engineering.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationCompletionForTeamView;
import com.cloudsigns.factorymanager2.engineering.adapter.PublicationCompletionOrderMeidaForSuperAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.PublicationOrderDetailBean;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationCompletionForTeamPresenter;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/30.
 */

public class TeamPublicationCompletionOrderFragment extends Fragment implements View.OnClickListener, IPublicationCompletionForTeamView {
    private ListView lv_meidalist;
    private PublicationCompletionOrderMeidaForSuperAdapter publicationCompletionOrderMeidaForSuperAdapter;
    private TextView edt_person_colon, edt__time_colon;
    private TextView btn_submit;
    private boolean isclickable;
    private final String TAG = TeamPublicationCompletionOrderFragment.class.getSimpleName();
    PublicationOrderDetailBean publicationOrderDetailBean;
    List<MediaBean> mediaBeanList = new ArrayList<>();
    private PublicationCompletionForTeamPresenter publicationCompletionForTeamPresenter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        publicationCompletionForTeamPresenter = new PublicationCompletionForTeamPresenter(this);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_publication_completion_order_for_manage, null);
        lv_meidalist = view.findViewById(R.id.lv_meidalist);

        edt_person_colon = view.findViewById(R.id.edt_person_colon);
        edt__time_colon = view.findViewById(R.id.edt__time_colon);

        btn_submit = view.findViewById(R.id.btn_submit);

        publicationCompletionOrderMeidaForSuperAdapter = new PublicationCompletionOrderMeidaForSuperAdapter(getActivity(), TAG);
        publicationCompletionOrderMeidaForSuperAdapter.setData(mediaBeanList);
        lv_meidalist.setAdapter(publicationCompletionOrderMeidaForSuperAdapter);

        btn_submit.setOnClickListener(this);


        return view;
    }

    public void initData(PublicationOrderDetailBean publicationOrderDetailBean) {
        if (null != publicationOrderDetailBean) {
            this.publicationOrderDetailBean = publicationOrderDetailBean;
            if (null != publicationOrderDetailBean) {
                edt_person_colon.setText(publicationOrderDetailBean.getAssignCompleteAccountName());
                edt__time_colon.setText(DateUtil.getYyyy_Mm_Dd(publicationOrderDetailBean.getAssignCompleteOrderTime()));

                List<MediaBean> list = publicationOrderDetailBean.getMediaList();
                if (null != list) {
                    mediaBeanList.clear();
                    mediaBeanList.addAll(list);

                    publicationCompletionOrderMeidaForSuperAdapter.setData(mediaBeanList);
                    publicationCompletionOrderMeidaForSuperAdapter.notifyDataSetChanged();
                }

            }


            CommonEnum.CompleteStatus completeStatus = publicationOrderDetailBean.getCompleteStatus();
            if (null != completeStatus) {
                if (completeStatus == CommonEnum.CompleteStatus.WORKING && publicationOrderDetailBean.getAssignStatus() == CommonEnum.AssignStatus.RECEIVED) {
                    btnClickable(true);
                } else if (completeStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                    btnClickable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    btnClickable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                    btnClickable(false);
                }
            }


        }
    }

    private void btnClickable(boolean bool) {
        isclickable = bool;
        if (bool) {
            btn_submit.setBackgroundResource(R.mipmap.btn_long_bg_green);
            btn_submit.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        } else {

            btn_submit.setBackgroundResource(R.mipmap.btn_bg_short_gray);
            btn_submit.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        }
    }


    private void submit() {
        if (null != publicationCompletionForTeamPresenter)
            publicationCompletionForTeamPresenter.publicationCommentFormanager();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                if (isclickable)
                    submit();
                break;
        }
    }

    @Override
    public String getOrderId() {
        if (null != publicationOrderDetailBean)
            return publicationOrderDetailBean.getId();
        return null;
    }

    @Override
    public void publicationCompletionForTeamSuccess() {
        Toast.makeText(getActivity(), "完工提交成功", Toast.LENGTH_SHORT).show();
        btn_submit.setVisibility(View.GONE);
    }

    @Override
    public void publicationCompletionForTeamFailed(String desc) {
        Toast.makeText(getActivity(), desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
